//
//  HLTfenleiTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/10/8.
//  Copyright © 2015年 hlt. All rights reserved.
//

#import "HLTfenleiTableViewController.h"
#import "HLTShopViewController.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "RecommendCell.h"
#import "SearchViewController.h"
#import "cloudsearchmapViewController.h"
@interface HLTfenleiTableViewController ()
{
    NSMutableArray *_recommendArray;
    NSMutableArray *_MerchantArray;
    NSString *_locationInfoStr;
    NSInteger _KindID;//分类查询ID，默认-1
    NSInteger _offset;
    
}


@end

@implementation HLTfenleiTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置返回的按钮的字体的颜色的
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    //设置导航的背景颜色的
    self.navigationController.navigationBar.barTintColor = navigationBarColor;
    _MerchantArray = [[NSMutableArray alloc] init];
    _recommendArray = [[NSMutableArray alloc] init];   //商铺数据
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self initViews];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)initViews{
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self setUpTableView];
    //去掉多余分割线的方法
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [self.tableView setTableFooterView:view];
}



-(void)setUpTableView{
    //添加下拉的动画图片
    //设置下拉刷新回调
    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
    
    //设置普通状态的动画图片
//    NSMutableArray *idleImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=60; ++i) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
//        [idleImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
//    
//    //设置即将刷新状态的动画图片
//    NSMutableArray *refreshingImages = [NSMutableArray array];
//    for (NSInteger i = 1; i<=3; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
//        [refreshingImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
//    
//    //设置正在刷新是的动画图片
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
    
    //马上进入刷新状态
    [self.tableView.gifHeader beginRefreshing];
    
    
    //上拉刷新
    [self.tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    //隐藏状态文字
    //    self.tableView.footer.stateHidden = YES;
    //设置正在刷新的动画
//    self.tableView.gifFooter.refreshingImages = refreshingImages;
    
    
    
    
}


-(void)getFirstPageData{
    _offset = 0;
    [self refreshData];
}

-(void)refreshData{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self getMerchantList];
    });
}

-(void)loadMoreData{
    NSLog(@"2222  offset:%zd",_offset);
    _offset = _offset + 10;
    NSLog(@"3333  offset:%zd",_offset);
    [self refreshData];
}


//获取商家列表
-(void)getMerchantList{
    //    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    
    
    
    
    NSInteger page1=_offset/10;
    //获取指定类型的商铺(name=商铺类型名，传全部类型可以查出全部&page=0&area=贵阳市内区域名，不传可以得到全部区域的内容)
    NSString *url=[zhidingcategoryshop stringByAppendingFormat:@"&name=%@&page=%ld&area=",self.categary,(long)page1];
    //    NSString *url=@"http://www.hltgz.com/wx/mobile/index.php?m=default&c=appinterface&a=recommend&page=0";
    NSLog(@"%@",url);
    
    [HLTNetWorkingtool getyouhuilist:url success:^(NSMutableArray *ary) {
        
        
        if (_offset<10) {
            [_recommendArray removeAllObjects];
        }
        
        
        for (int i = 0; i < ary.count; i++) {
            RecommendModel *recommend = [RecommendModel objectWithKeyValues:ary[i]];
            [_recommendArray addObject:recommend];
        }
        [self.tableView reloadData];
        //刷新指定的section,显示推荐商铺的部分
        //        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
        //        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    } faliure:^(NSString *error) {
        
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
        
    }];
    
    
    
    
    
}





#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _recommendArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *cellIndentifier = @"recommendcell";
    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[RecommendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    RecommendModel *recommend = _recommendArray[indexPath.row];
    [cell setRecommendData:recommend];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
    
    
}



#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    RecommendModel *recommend = _recommendArray[indexPath.row];
    //                    [recommend objectArray2keyValuesArray]
    //                    NSString *shopId = recommend.Shop_id;
    //            NSLog(@"shop id:%@",shopId);
    
    HLTShopViewController *shopvc=[[HLTShopViewController alloc]init];
    shopvc.shopcontent=recommend;
    shopvc.biaozhi=YES;
    //隐藏tabbar
    shopvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:shopvc animated:YES];
    //                    HLTShopcontentViewController *shopVC = [[HLTShopcontentViewController alloc] init];
    //                    shopVC.imagearray=imagearray;
    //                    shopVC.shopID = shopId;
    //                    shopVC.shopcontent=recommend;
    //                    [self.navigationController pushViewController:shopVC animated:YES];
    
    
    
    
    
    
    
}





//将要出现的时候
- (void)viewWillAppear:(BOOL)animated
{
    //打开导航
    self.navigationController.navigationBarHidden=NO;
    
}








@end
